//
//  KKPageControllerModel.m
//  Butler
//
//  Created by liaozhenming on 16/6/8.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKPageControllerModel.h"

#import "CustomerCollectionViewCell.h"
#import "GoodsCollectionViewCell.h"
#import "EmployeeCollectionViewCell.h"

@interface KKPageChildViewController()

@property (nonatomic, strong) UIImageView* backgroundImageView;

@end

@implementation KKPageChildViewController

- (void)setPageIndex:(NSInteger)pageIndex {
    _pageIndex = pageIndex;
    
    if (_pageIndex % 2 == 0) {
        _backgroundImageView.image = [UIImage imageNamed: @"left_page"];
    }
    else {
        _backgroundImageView.image = [UIImage imageNamed: @"right_page"];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *rootView = self.view;
    
    _backgroundImageView = [[UIImageView alloc] initWithFrame: [rootView bounds]];
    _backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _backgroundImageView.contentMode = UIViewContentModeCenter;
    [rootView addSubview: _backgroundImageView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(160, 120);
    layout.minimumLineSpacing = 20;
    layout.sectionInset = UIEdgeInsetsMake(20, 34, 0, 34);
    
    _collectionView = [[UICollectionView alloc] initWithFrame: [rootView bounds] collectionViewLayout: layout];
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [rootView addSubview: _collectionView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear: animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear: animated];
}

#pragma mark - UICollectionViewDelegate methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.type == KKPageChildViewControllerTypeCustomer) {
        CustomerCollectionViewCell *cell = [CustomerCollectionViewCell kk_cellWithCollectionView: collectionView indexPath: indexPath];
        return cell;
    }
    else if (self.type == KKPageChildViewControllerTypeGoods) {
        GoodsCollectionViewCell *cell = [GoodsCollectionViewCell kk_cellWithCollectionView: collectionView indexPath: indexPath];
        return cell;
    }
    else if (self.type == KKPageChildViewControllerTypeUser) {
        EmployeeCollectionViewCell *cell = [EmployeeCollectionViewCell kk_cellWithCollectionView: collectionView indexPath: indexPath];
        return cell;
    }
    return nil;
}


@end

#pragma mark - KKPageControllerModel methods
@implementation KKPageControllerModel

- (KKPageChildViewController*)viewControllerWithIndex:(NSInteger)index {
    
    KKPageChildViewController *viewController = [KKPageChildViewController new];
    viewController.type = self.type;
    viewController.view.frame = CGRectMake(0, 0, 434, 585);
//    viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    viewController.pageIndex = index;
    return viewController;
}

@end
